package tjx.Filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

//记录用户的请求地址和请求时间
//@WebFilter(filterName = "LogFilter",urlPatterns = "/*")
public class Filter1_LogFilter implements Filter {
    private FilterConfig config;
    public void init(FilterConfig config)
    {
        this.config=config;
    }
    public void destroy()
    {
        this.config=null;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
    {
        ServletContext context=this.config.getServletContext();
        HttpServletRequest r=(HttpServletRequest)request;
        String path=r.getContextPath();
        long before=System.currentTimeMillis();
        System.out.println("开始过滤...");
        System.out.println("Filter1_LogFilter 开始写日志,用户访问时间："+new Date()+";用户访问地址："+path);

        HttpServletRequest hrequest=(HttpServletRequest)request;
        System.out.println("Filter 已经截获到用户的请求的地址："+hrequest.getServletPath());
        chain.doFilter(request,response);
        System.out.println("Filter0_LoginFilter chain.doFilter()以后");
        long after =System.currentTimeMillis();
        System.out.println("过滤结束");
        System.out.println("请求被定位到"+hrequest.getRequestURI()+"所花的时间为："+(after-before));
    }

//    public void init(FilterConfig config) throws ServletException {
//
//    }
}
